package com.youth.fund.hk.hostManager.lucene.webcontent;

import java.io.IOException;
import java.sql.Timestamp;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.util.Version;

import com.youth.fund.hk.hostManager.lucene.datamodel.ContentModel;

/**
 * @author SunkSky
 *
 *         2014年11月24日上午1:47:19
 */
public class PackContentObject
{

	/**
	 * 转换内容对象为Document对象
	 * 
	 * @param ct
	 * @return
	 */
	public static Document convertContentToDoc(ContentModel ct)
	{
		Document doc = new Document();
		// Field.Store.YES 表示存储内容
		doc.add(new StringField("title", transferObject(ct.getTitle()), Field.Store.YES));
		doc.add(new TextField("content", transferObject(ct.getContent()), Field.Store.YES));
		doc.add(new TextField("url", transferObject(ct.getUrl()), Field.Store.YES));
		doc.add(new TextField("createDate", new Timestamp(System
				.currentTimeMillis()).toString(), Field.Store.YES));
		return doc;
	}
	
	private static String transferObject(Object o)
	{
		String result = "";
		
		if(o !=null)
		{
			result = o.toString();
		}
		
		return result;
	}

	/**
	 * 转换Documnet对象为内容对象
	 * 
	 * @param doc
	 * @return
	 */
	public static ContentModel convertDocToContent(Document doc)
	{
		ContentModel ct = new ContentModel();
		ct.setTitle(doc.get("title"));
		ct.setContent(doc.get("content"));
		ct.setUrl(doc.get("url"));
		Timestamp dt = Timestamp.valueOf((doc.get("createDate")));
		ct.setCreateTime(dt);
		return ct;
	}

	/**
	 * 转换Documnet对象为内容对象 针对关键字加亮
	 * 
	 * @param doc
	 * @return
	 */
	public static ContentModel convertDocToContentWithLighter(Document doc, Query query,
			Analyzer analyzer)
	{
		ContentModel ct = new ContentModel();
		// ct.setTitle(highligher(doc.get("title"),query,"title"));
		ct.setTitle(doc.get("title"));

		ct.setContent(highligher(doc.get("content"), query, "content", analyzer));
		//ct.setContent(doc.get("content"));
		ct.setUrl(doc.get("url"));
		Timestamp dt = Timestamp.valueOf(doc.get("createDate"));
		ct.setCreateTime(dt);
		return ct;
	}

	/**
	 * Function:关键字加亮
	 * 
	 * @author JLC
	 * @param text
	 *            原内容
	 * @param query
	 *            搜索的关键字
	 * @param field
	 *            要加亮的栏位
	 * @return
	 */
	private static String highligher(String text, Query query, String field,
			Analyzer analyzer)
	{
		try
		{
			QueryScorer scorer = new QueryScorer(query);
			Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
			// 关键字被包住的内容
			Formatter formatter = new SimpleHTMLFormatter(
					"<b>", "</b>");
			Highlighter lighter = new Highlighter(formatter, scorer);
			lighter.setTextFragmenter(fragmenter);
			String ht = lighter.getBestFragment(analyzer, field, text);
			if (ht == null)
			{
				// 当内容超过两百字时，截取字符串
				if (text.length() >= 200)
				{
					text = text.substring(0, 200);
					text = text + "....";
				}
				return text;
			} else
				return ht.trim();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (InvalidTokenOffsetsException e)
		{
			e.printStackTrace();
		}
		return text;
	}
}